package com.lzb.compressmaster.activity;

import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.UtilsTransActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.compressmaster.BaseAc;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.adapter.SelPictureAdapter;
import com.lzb.compressmaster.basic.picselect.MediaLoadManager;
import com.lzb.compressmaster.basic.picselect.PhotoConfig;
import com.lzb.compressmaster.basic.picselect.SelectMediaEntity;
import com.lzb.compressmaster.databinding.ActivitySelPictureBinding;
import com.lzb.compressmaster.dialog.PermissionExplainDialog;
import com.lzb.compressmaster.utils.RxUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.core.ObservableEmitter;

public class SelPictureActivity extends BaseAc<ActivitySelPictureBinding> implements View.OnClickListener, OnItemClickListener {

    private SelPictureAdapter pictureAdapter;
    private List<String> listSel = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_sel_picture;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSelPictureBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvPicSizeCompress.setOnClickListener(this);
        mDataBinding.tvPackageCompress.setOnClickListener(this);

        mDataBinding.rvPictureList.setLayoutManager(new GridLayoutManager(mContext, 3));
        pictureAdapter = new SelPictureAdapter();
        mDataBinding.rvPictureList.setAdapter(pictureAdapter);
        pictureAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvPicSizeCompress) {//图片大小压缩
            if (listSel.size() == 0) {
                ToastUtils.showShort(getString(R.string.please_sel_picture));
                return;
            }
            PictureCompressActivity.listPath.clear();
            PictureCompressActivity.listPath.addAll(listSel);
            startActivity(PictureCompressActivity.class);
        } else if (id == R.id.tvPackageCompress) {//压缩包格式压缩
            if (listSel.size() == 0) {
                ToastUtils.showShort(getString(R.string.please_sel_picture));
                return;
            }
            FileCompressActivity.listPath = listSel;
            startActivity(FileCompressActivity.class);
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (pictureAdapter.getItem(position).isChecked()) {
            pictureAdapter.getItem(position).setChecked(false);
            listSel.remove(pictureAdapter.getItem(position).getPath());
        } else {
            pictureAdapter.getItem(position).setChecked(true);
            listSel.add(pictureAdapter.getItem(position).getPath());
        }
        pictureAdapter.notifyItemChanged(position);
        mDataBinding.tvSelNum.setText(getString(R.string.sel_num, listSel.size()));
    }

    @Override
    protected void initData() {
        mDataBinding.tvSelNum.setText(getString(R.string.sel_num, 0));
        getPermission();
    }

    private void getPermission() {
        PermissionExplainDialog dialog = new PermissionExplainDialog(mContext);
        dialog.setContent(getString(R.string.get_picture_permission));
        List<String> permissionsInManifest = new ArrayList<>();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            permissionsInManifest.add(Manifest.permission.READ_MEDIA_IMAGES);
            permissionsInManifest.add(Manifest.permission.READ_MEDIA_VIDEO);
        } else {
            permissionsInManifest.add(Manifest.permission.READ_EXTERNAL_STORAGE);
            permissionsInManifest.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
        PermissionUtils.permission(permissionsInManifest.toArray(new String[permissionsInManifest.size()])).explain(new PermissionUtils.OnExplainListener() {
            @Override
            public void explain(@NonNull UtilsTransActivity activity, @NonNull List<String> denied, @NonNull ShouldRequest shouldRequest) {
                dialog.show();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        shouldRequest.start(true);
                    }
                },500);
            }
        }).callback(new PermissionUtils.SimpleCallback() {
            @Override
            public void onGranted() {
                dialog.dismiss();
                getData();
            }

            @Override
            public void onDenied() {
                dialog.dismiss();
//                try {
//                    AppUtils.launchAppDetailsSettings();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
            }
        }).request();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.PHOTO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    pictureAdapter.setList(selectMediaEntities);
                    mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
                    mDataBinding.flPictureNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvPictureList.setVisibility(View.GONE);
                    mDataBinding.flPictureNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}